-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11 - part1.py
More file actions
54 lines (43 loc) · 1.34 KB
/
day11 - part1.py
File metadata and controls
54 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
file = open("input.txt")
grid = []
neighbors = [(+1, 0), (-1, 0), (0, +1), (0, -1), (+1, +1), (-1, -1), (+1, -1), (-1, +1)]
flashes_count = 0
class Octopus:
def __init__(self, x, y, energy):
self.x = x
self.y = y
self.energy = energy
self.has_flashed = False
def increase(self):
global flashes_count
if self.has_flashed:
return
self.energy += 1
if self.energy > 9:
self.energy = 0
self.has_flashed = True
flashes_count += 1
for x, y in neighbors:
n_x = self.x + x
n_y = self.y + y
if (
n_x < 0
or n_x > len(grid[self.y]) - 1
or n_y < 0
or n_y > len(grid) - 1
):
continue
grid[n_y][n_x].increase()
for y, line in enumerate(file.readlines()):
octopuses = []
for x, o in enumerate(line.replace("\n", "")):
octopuses.append(Octopus(x, y, int(o)))
grid.append(octopuses)
for step in range(100):
for line in grid:
for octo in line:
octo.increase()
for line in grid:
for octo in line:
octo.has_flashed = False
print(flashes_count)